#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
处理trans.txt文件，对每一行进行命名实体识别并保存结果
"""

import requests
import json
import time
from typing import List, Dict, Any

def read_trans_file(file_path: str = "trans.txt") -> List[str]:
    """
    读取trans.txt文件中的所有行
    
    Args:
        file_path: 文件路径
        
    Returns:
        文本行列表
    """
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            lines = [line.strip() for line in f.readlines() if line.strip()]
        return lines
    except FileNotFoundError:
        print(f"文件 {file_path} 未找到")
        return []
    except Exception as e:
        print(f"读取文件时出错: {e}")
        return []

def analyze_text_bert(text: str, domain: str = "general") -> Dict[str, Any]:
    """
    使用BERT方法分析文本
    
    Args:
        text: 要分析的文本
        domain: 领域
        
    Returns:
        分析结果
    """
    try:
        response = requests.post(
            "http://localhost:5000/nlp/analysisText",
            json={
                "systemCode": "NLP_SYSTEM_001",
                "text": text,
                "domain": domain,
                "method": "bert"
            },
            timeout=30
        )
        
        if response.status_code == 200:
            return response.json()
        else:
            print(f"BERT分析失败，状态码: {response.status_code}, 响应: {response.text}")
            return {"error": f"Status code: {response.status_code}"}
    except Exception as e:
        print(f"BERT分析出错: {e}")
        return {"error": str(e)}

def analyze_text_advanced(text: str, domain: str = "general") -> Dict[str, Any]:
    """
    使用增强版方法分析文本
    
    Args:
        text: 要分析的文本
        domain: 领域
        
    Returns:
        分析结果
    """
    try:
        response = requests.post(
            "http://localhost:5000/nlp/analysisText",
            json={
                "systemCode": "NLP_SYSTEM_001",
                "text": text,
                "domain": domain,
                "method": "advanced"
            },
            timeout=30
        )
        
        if response.status_code == 200:
            return response.json()
        else:
            print(f"增强版分析失败，状态码: {response.status_code}, 响应: {response.text}")
            return {"error": f"Status code: {response.status_code}"}
    except Exception as e:
        print(f"增强版分析出错: {e}")
        return {"error": str(e)}

def process_trans_file():
    """
    处理trans.txt文件中的每一行
    """
    # 读取文件
    lines = read_trans_file("trans.txt")
    if not lines:
        print("没有读取到任何文本行")
        return
    
    print(f"共读取到 {len(lines)} 行文本")
    
    # 存储结果
    bert_results = []
    advanced_results = []
    
    # 处理每一行
    for i, line in enumerate(lines):
        print(f"处理第 {i+1}/{len(lines)} 行: {line[:50]}...")
        
        # BERT方法分析
        bert_result = analyze_text_bert(line, "manufacturing")
        bert_results.append({
            "line_number": i+1,
            "text": line,
            "result": bert_result
        })
        
        # 增强版方法分析
        advanced_result = analyze_text_advanced(line, "manufacturing")
        advanced_results.append({
            "line_number": i+1,
            "text": line,
            "result": advanced_result
        })
        
        # 避免请求过于频繁
        time.sleep(0.5)
    
    # 保存BERT结果
    try:
        with open("trans_bert_results.json", "w", encoding="utf-8") as f:
            json.dump(bert_results, f, ensure_ascii=False, indent=2)
        print("BERT结果已保存到 trans_bert_results.json")
    except Exception as e:
        print(f"保存BERT结果时出错: {e}")
    
    # 保存增强版结果
    try:
        with open("trans_advanced_results.json", "w", encoding="utf-8") as f:
            json.dump(advanced_results, f, ensure_ascii=False, indent=2)
        print("增强版结果已保存到 trans_advanced_results.json")
    except Exception as e:
        print(f"保存增强版结果时出错: {e}")

def print_sample_results():
    """
    打印示例结果
    """
    try:
        # 读取并显示BERT结果示例
        with open("trans_bert_results.json", "r", encoding="utf-8") as f:
            bert_results = json.load(f)
        
        print("\n=== BERT方法示例结果 ===")
        for i, item in enumerate(bert_results[:3]):  # 显示前3个结果
            print(f"行 {item['line_number']}: {item['text']}")
            if "ners" in item["result"]:
                for entity in item["result"]["ners"]:
                    print(f"  - {entity['nerKey']}: {entity['nerValue']} (置信度: {entity.get('confidence', 'N/A')})")
            print()
            
        # 读取并显示增强版结果示例
        with open("trans_advanced_results.json", "r", encoding="utf-8") as f:
            advanced_results = json.load(f)
            
        print("=== 增强版方法示例结果 ===")
        for i, item in enumerate(advanced_results[:3]):  # 显示前3个结果
            print(f"行 {item['line_number']}: {item['text']}")
            if "ners" in item["result"]:
                for entity in item["result"]["ners"]:
                    source = entity.get('source', 'unknown')
                    print(f"  - {entity['nerKey']}: {entity['nerValue']} (置信度: {entity.get('confidence', 'N/A')}, 来源: {source})")
            print()
            
    except FileNotFoundError:
        print("结果文件未找到，请先运行处理脚本")
    except Exception as e:
        print(f"读取结果文件时出错: {e}")

if __name__ == "__main__":
    print("开始处理trans.txt文件...")
    process_trans_file()
    print("\n处理完成，显示示例结果:")
    print_sample_results()